
import json
import collections
import os, re

#============================================================================
def noun_plural(word):

    # 以辅音字母加'y '结尾的动词, 用'ies '代替'y '构成's '形式,
    if re.search("[^aeiou]y$", word):
        return re.sub("y$", "ies", word), 0

    ## f或fe，结尾的名词，数量不多，构成复数时，用ves
    #if re.search("fe$|f$", word):
    #    return re.sub("fe$|f$", "ves", word), 1
    #
    ## 某些以us结尾的名词，复数词尾是'i'.
    #if re.search("us$", word):
    #    return re.sub("us$", "i", word), 2
    #
    ## 某些以'um，结尾的名词，复数词尾是a.
    #if re.search("um$", word):
    #    return re.sub("um$", "a", word), 3

    # 大多数以，is，结尾的名词，构成复数用'es，代替'is'.
    if re.search("is$", word):
        return re.sub("is$", "es", word), 4

    # sh, ss, x, s等字母后，复数要加'e
    if re.search("sh$|ss$|x$|s$", word):
        return word + "es", 5

    ## 些以'a'结尾的名词，构成复数时词尾加e
    #if re.search("a$", word):
    #    return re.sub("a$", "ae", word), 6

    # 多数情况下，复数要加's'.
    return word+"s", 7

#============================================================================
f = open("e:\\w\\forms-noun.csv", 'r' ,encoding='utf8')
fo = open("e:\\w\\forms-noun-irregular.csv", 'w' ,encoding='utf8')
fword = open("e:\\w\\noun_list.csv", 'w' ,encoding='utf8')


words = {}

for line in f.readlines():
    items=re.split("\|",line)
    word                  =  items[0].strip() if len(items) >= 1 else None
    plural    =  items[1].strip() if len(items) >= 2  else ""

    fword.write(word+"\n")

    form_from_book = plural
    if len(form_from_book) :
        word_normal_form, kind = noun_plural(word)
        p_items =re.split(",",form_from_book)
        found=False
        for pi in p_items:
            if  word_normal_form == pi:
                #fo.write("~ "+str(kind)+"\n")
                found=True
                pass
                break

        if found is False:
            #print(word +"|" + form_from_book +"|"+ word_normal_form)
            #fo.write(str(kind)+"\n")
            pass
        else:
            plural="_"

    if (plural and plural != "_"):
        fo.write(word+"|"+"{}\n".format(plural ))
        pass
    else:
        #fo.write("{}\n".format(plural ))
        pass
fo.close()

fword.close()

